package demo.dao;

import jdbc.DBUtil;
import jdbc.UserInfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserMapper {

    //插入一个用户，返回修改的行数
    public int insert(UserInfo userinfo) {
        try (Connection connection = DBUtil.getConnection()) {
            PreparedStatement ps = connection.prepareStatement("insert into userinfo (username ,password ,nickname, age) " +
                    "values (?,?,?,?)");
            ps.setString(1, userinfo.getUsername());
            ps.setString(2, userinfo.getPassword());
            ps.setString(3, userinfo.getNickname());
            ps.setInt(4, userinfo.getAge());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

    //输入一个用户名，返回用户的信息
    public UserInfo findByUsername(String username){
        try (Connection connection = DBUtil.getConnection()) {
            //验证用户是否为为重复信息
            PreparedStatement ps = connection.prepareStatement("select username , password ,nickname ,age " +
                    "from userinfo " +
                    "where username =? ");
            ps.setString(1,username);
            ResultSet resultSet = ps.executeQuery();
            if (resultSet.next()){
                UserInfo userInfo = new UserInfo();
                userInfo.setUsername(resultSet.getString("username"));
                userInfo.setPassword(resultSet.getString("password"));
                userInfo.setNickname(resultSet.getString("nickname"));
                userInfo.setAge(resultSet.getInt("age"));
                return userInfo;
            }

        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }

    //传入一个用户名和一个新密码,修改密码
    public int update(UserInfo userInfo) {
        try (Connection connection = DBUtil.getConnection()) {
            PreparedStatement ps = connection.prepareStatement("update userinfo set password= ?,nickname= ?, age= ? where username = ? " );
            ps.setString(1,userInfo.getPassword());
            ps.setString(2, userInfo.getNickname());
            ps.setInt(3, userInfo.getAge());
            ps.setString(4, userInfo.getUsername());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //判断用户名和密码是否正确
    public UserInfo findByUsername_password(String username,String password){
        try (Connection connection = DBUtil.getConnection()) {
            //验证用户是否为为重复信息
            PreparedStatement ps = connection.prepareStatement("select username , password ,nickname ,age " +
                    "from userinfo " +
                    "where username =? and password =? ");
            ps.setString(1,username);
            ps.setString(2,password);
            ResultSet resultSet = ps.executeQuery();
            if (resultSet.next()){
                UserInfo userInfo = new UserInfo();
                userInfo.setUsername(resultSet.getString("username"));
                userInfo.setPassword(resultSet.getString("password"));
                userInfo.setNickname(resultSet.getString("nickname"));
                userInfo.setAge(resultSet.getInt("age"));
                return userInfo;
            }

        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }


}
